$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'exam/examinfo/waitExamList',
        datatype: "json",
        colModel: [			
			{ label: 'optionId', name: 'examId', index: 'exam_id', width: 50, key: true, hidden: true },
			{ label: '考试名称', name: 'examTitle', index: 'exam_title', width: 300 },
            // { label: '考试状态', name: 'optionContent', index: 'option_content', width: 80 },	
            { label: '操作', name: 'optionContent', index: 'option_content', width: 80,
                formatter: (cellValue,option,rowObject)=> {
                    return `<a class="btn" onclick="examStart(${rowObject.examId})"><i class="fa fa-pencil-square-o"></i>&nbsp;开始考试</a>`
                }
            }				
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: false, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		examOption: {},
        subjectList: {},  // 题目列表
        myOptionList: [], // 我的答案列表
        answerList: [], // 答案列表
        examInfo: {},
        thisscore: '',
        maxtime: 60*60,
        countDownTime: ''
	},
    watch: {
        "maxtime" : function(oldVal,NewVal) {
            if (NewVal == 300) {
                alert("考试还剩五分钟")
            }
            if (NewVal == 0) {
                alert("考试结束自动交卷")
                this.submitOver()
            }
        }
    },
	methods: {
		query: function () {
			vm.reload();
		},
		exambegin: function(examId){
			vm.showList = false;
			vm.title = "开始考试";
			vm.examOption = {};
            vm.getSubjectInfo(examId)
            setInterval(vm.countDownTimeStart, 1000);
		},
        countDownTimeStart: function(){

            minutes = Math.floor(this.maxtime / 60);
            seconds = Math.floor(this.maxtime % 60);
            vm.maxtime -= 1
            valueMsg = `距离结束还有${minutes}分${seconds}秒;`
            vm.countDownTime = valueMsg

        },
		submitOver: function (event) {
            for(let i in vm.subjectList){
                vm.answerList.push(parseInt(vm.subjectList[i].examSubjectEntity.subjectAnswer))
            }
            var scoreFalg = 0
            for(let j in vm.answerList){
                vm.answerList[j]  // 标准答案
                vm.myOptionList[j] // 我的答案
                if(vm.answerList[j] === vm.myOptionList[j]) {
                    scoreFalg ++
                }
            }
            this.thisscore = scoreFalg*5
            let data = {
                examTitle: vm.examInfo.examTitle,
                examScores: this.thisscore
            }
            console.log(data)
            var url = "exam/examscore/submitOver";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(data),
                success: function(r){
                    if(r.code === 0){
                        alert("本次考试成绩" + vm.thisscore + "分",()=>{
                            location.reload()
                        })
                    }else{
                        layer.alert(r.msg);
                    }
                }
            }); 
		},
		getInfo: function(optionId){
			$.get(baseURL + "exam/examoption/info/"+optionId, function(r){
                vm.examOption = r.examOption;
            });
		},
        getSubjectInfo: function(tpId){
			$.get(baseURL + "exam/examinfo/examBegin/"+tpId, function(r){
                vm.subjectList = r.subjectList;
                vm.examInfo = r.examInfo
                for(let i=0;i<vm.subjectList.length;i++){
                    vm.myOptionList[i] = 0
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});
function examStart(examId) {
    console.log("开始考试")
    vm.exambegin(examId)
}
